package geektime.spring.springbucks.customerservice;

import geektime.spring.springbucks.customerservice.integration.CoffeeOrderService;
import geektime.spring.springbucks.customerservice.integration.CoffeeService;
import geektime.spring.springbucks.customerservice.model.Coffee;
import geektime.spring.springbucks.customerservice.model.CoffeeOrder;
import geektime.spring.springbucks.customerservice.model.NewOrderRequest;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CustomerRunner implements ApplicationRunner {

  @Autowired
  private CoffeeService coffeeService;

  @Autowired
  private CoffeeOrderService coffeeOrderService;

  @Override
  public void run(ApplicationArguments args) throws Exception {
    readMenu();
    Long id = orderCoffee();
    queryOrder(id);
  }


  private void readMenu() {
    List<Coffee> coffees = coffeeService.getAll();
    coffees.forEach(c -> log.info("Coffee :{}", c));
  }

  private Long orderCoffee() {
    NewOrderRequest orderRequest = NewOrderRequest.builder()
        .customer("Li Lei")
        .items(Arrays.asList("capuccino"))
        .build();

    CoffeeOrder order = coffeeOrderService.create(orderRequest);

    Long id = order.getId();
    log.info("Order Id:{}", id);
    return id;
  }

  private void queryOrder(Long id) {
    CoffeeOrder order = coffeeOrderService.getOrder(id);
    log.info("Order:{}", order);
  }
}
